-- implement sieve of erastosthenes -- using concurrent actors
actorSieve is package{
  import concurrency;
 
  filterActor(P) is concurrent actor{
    private var Nx := (function(_) is task{});
    
    {Nx := newPrime};
    
    private newPrime(X) is let{
      Fx is filterActor(X);
      
      filterPrime(XX) is task{ notify Fx with XX on input};
    } in task {
      logMsg(info,"new prime $X");
      Nx := filterPrime;
    };
    
    on X on input do {
      perform task {
        if X%P!=0 then
          perform Nx(X);
      }
    }
  }

  S is filterActor(2);
  
  main() do {
    perform task{
      for Ix in iota(3,1000,2) do{
        -- logMsg(info,"next number is $Ix");
        notify S with Ix on input
      }
    }
  }
}
 